# scale_tree/__init__.py
class QuestionNode:
    def __init__(self, question_id, options=None, sub_questions=None):
        self.question_id = question_id
        self.options = options if options else []
        self.sub_questions = sub_questions if sub_questions else {}

    def add_option(self, option, next_question):
        self.options.append(option)
        self.sub_questions[option] = next_question

    def __repr__(self):
        return f"QuestionNode(question_id={self.question_id}, options={self.options}, sub_questions={self.sub_questions})"


def parse_structure(structure, question_id=1):
    if not structure:
        return None

    node = QuestionNode(question_id)
    question_id += 1

    for option in structure:
        if isinstance(option, list):
            sub_node = parse_structure(option, question_id)
            node.add_option(option, sub_node)
        else:
            node.add_option(option, None)
            question_id += 1

    return node


def build_question_tree(structure):
    return parse_structure(structure)


def ask_questions(node):
    if not node:
        return

    print(f"Question {node.question_id}:")
    for i, option in enumerate(node.options):
        print(f"{i + 1}. Option {option}")

    answer = int(input("Choose an option: ")) - 1
    chosen_option = node.options[answer]

    if chosen_option in node.sub_questions:
        ask_questions(node.sub_questions[chosen_option])


# 示例嵌套结构
structure = [2, 3, [[3, 5], 0, 0], 2, 5]

# 构建问题树
question_tree = build_question_tree(structure)

# 开始问问题
ask_questions(question_tree)
